package org.example.myleet.p535;

import java.util.HashMap;
import java.util.Map;

public class Codec {

    private Map<Integer, String> aliasMapUrl = new HashMap<>();

    // Encodes a URL to a shortened URL.
    public String encode(String longUrl) {
        int alias = longUrl.hashCode();
        while (aliasMapUrl.containsKey(alias)) {
            ++alias;
        }
        aliasMapUrl.put(alias, longUrl);
        return alias + "";
    }

    // Decodes a shortened URL to its original URL.
    public String decode(String shortUrl) {
        Integer alias = Integer.parseInt(shortUrl);
        return aliasMapUrl.get(alias);
    }
}
